
$.fn.dialog=function(options){
	
	var defaults={
		       title:'title',
			   content:"测试代码",
			   width:400,
               height:300,
			   left:50,
			   top:50,
			   opacity:0.4
		},_this=this;
	
	_this.options=$.extend({},defaults,options);
	
	var diglog_html="<div id='dvMsgbox'><div id='dvMsgHead'><span class='fn-bg rc-l'></span><h4>"+_this.options.title+"</h4><a title='关闭' hidefocus='true' class='fn-bg Aclose' id='lnkClose' href='javascript:void(0);'>关闭</a></div><div id='divContent'></div></div>";
  
  
   this.showBox=function()
   {
	   $("#dvOverLay").remove();
	   $('#dvMsgbox').remove();
	   //$(diglog_html).appendTo('body').fadeIn(300);
	    _this.bg=$("<div id='dvOverLay' class='dvOverLay_hidn'></div>").hide().addClass('overLayBG').css('opacity', _this.options.opacity).dblclick(function(){
        _this.close();}).appendTo('body').fadeIn(300);
	   _this.content=$(diglog_html).appendTo('body');
	   innerContent();
	   setPosition();
	   setBind();
	   drag();
   }
   
   function innerContent()
   {
	   var content = _this.content.find("#divContent");
	   	   content.html(defaults.content);
   }
   function setPosition()
   {
       $('#dvMsgbox').css({width:_this.options.width,height:_this.options.height,top:_this.options.top,left:_this.options.left});	
   }
   function setBind()
   {
	   $('#lnkClose').click(function(){
        _this.close();
       });
   }
   function drag()
   {
	   var dx,dy,moveout;
	   var title=_this.content.find("#dvMsgHead");
	   
	   title.mousedown(function(e){
		    dx=e.clientX-parseInt(_this.content.css('left'));
			dy=e.clientY-parseInt(_this.content.css('top'));
			_this.content.mousemove(move).mouseout(out).css('opacity', 0.9);
			title.mouseup(up);
		})
   
	   function move(e)
	   {
		   moveout=false;
		   if(e.clientX-dx<0)
		   {
			   var l=0;
		   }
		   else
		   {
			   if (e.clientX - dx > $(window).width() - _this.content.width()) {
					l = $(window).width() - _this.content.width();
				}
				else {
					l = e.clientX - dx
				}
		   }
		   //_top=(e.clientY - dy)<0?0:dy;
		   _this.content.css({
						left: l,
						top:e.clientY - dy
		   });
	   }
	   
	   function out(e)
	   {
			moveout = true;
			setTimeout(function(){
				moveout && up(e);
			}, 10);
	   }
	   
	   function up(e)
	   {
			_this.content.unbind("mousemove", move).unbind("mouseout", out).css('opacity', 1);
			 title.unbind("mouseup", up);
	   }
	 
   }
   
   this.close=function (){
         if (_this.content) {
                _this.bg.remove();
                _this.content.stop().fadeOut(300, function(){
                    _this.content.remove();
                })
            }
    }
   _this.showBox();
}